package jp.co.dailyReport.dailyReport.sql;

import java.util.Calendar;
import java.sql.Date;
import java.sql.Time;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import org.apache.log4j.Logger;
import jp.co.dailyReport.Utilities;
import jp.co.dailyReport.Executer;
import jp.co.dailyReport.dailyReport.DailyReport;

/**
 * 日報情報を更新するクラスです。
 */
public class DailyReportUpdateExecuter implements Executer {

	private static Logger log = Logger.getLogger(DailyReportUpdateExecuter.class);
	private DailyReport dailyReport;

	public DailyReportUpdateExecuter(DailyReport dailyReport) {
		this.dailyReport = dailyReport;
	}

	/**
	 * 日報IDを基に、日報情報を更新します。
	 * 
	 * @param conn コネクションオブジェクト
	 * @return null
	 * @throws SQLException
	 */
	public Object[] execute(Connection conn) throws SQLException {

		PreparedStatement pstmt = null;
		String sql = "update dailyReport set staffId=?,workDate=?,workingStartHours=?," +
			"workingEndHours=?,totalWorkingHours=?,workDetail=?,problem=?,toDo=?,comment=?," +
			"upDay=? where dailyReportId=?";
			log.debug("sql: " + sql);
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, dailyReport.getStaffId());
			pstmt.setDate(2, new Date(dailyReport.getWorkDate().getTime().getTime()));
			pstmt.setTime(3, new Time(dailyReport.getWorkingStartHours().getTime().getTime()));
			pstmt.setTime(4, new Time(dailyReport.getWorkingEndHours().getTime().getTime()));
			pstmt.setDouble(5, dailyReport.getTotalWorkingHours());
			pstmt.setString(6, Utilities.convUniqueCharacter(dailyReport.getWorkDetail()));
			pstmt.setString(7, Utilities.convUniqueCharacter(dailyReport.getProblem()));
			pstmt.setString(8, Utilities.convUniqueCharacter(dailyReport.getToDo()));
			pstmt.setString(9, Utilities.convUniqueCharacter(dailyReport.getComment()));
			Calendar cal = Calendar.getInstance();
			pstmt.setDate(10, new Date(cal.getTime().getTime()));
			pstmt.setInt(11, dailyReport.getDailyReportId());
			pstmt.execute();
		} finally {
			if(pstmt != null)
			 pstmt.close();
		}
		return null;
	}
}